import 'package:permission_handler/permission_handler.dart';

class RequestPermission {
  // 获取位置权限
  static Future<bool> requestLocationPermission() async {
    //获取当前的权限
    var locationStatus = await Permission.location.status;
    var locationAlwaysStatus = await Permission.locationAlways.status;
    var locationWhenInUseStatus = await Permission.locationWhenInUse.status;
    var assessMediaLocationStatus = await Permission.accessMediaLocation.status;

    if (locationStatus == PermissionStatus.granted ||
        locationAlwaysStatus == PermissionStatus.granted ||
        locationWhenInUseStatus == PermissionStatus.granted ||
        assessMediaLocationStatus == PermissionStatus.granted) {
      //已经授权
      return true;
    } else {
      //未授权则发起一次申请
      locationStatus = await Permission.location.request();
      locationAlwaysStatus = await Permission.locationAlways.request();
      locationWhenInUseStatus = await Permission.locationWhenInUse.request();
      assessMediaLocationStatus =
          await Permission.accessMediaLocation.request();

      if (locationStatus == PermissionStatus.granted ||
          locationAlwaysStatus == PermissionStatus.granted ||
          locationWhenInUseStatus == PermissionStatus.granted ||
          assessMediaLocationStatus == PermissionStatus.granted) {
        return true;
      } else {
        return false;
      }
    }
  }

  // 获取获取媒体位置权限
  static Future<bool> requestAssessMediaLocationPermission() async {
    //获取当前的权限
    var status = await Permission.accessMediaLocation.status;

    if (status == PermissionStatus.granted) {
      //已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.accessMediaLocation.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        return false;
      }
    }
  }

  // 获取蓝牙权限
  static Future<bool> requestBlueToothPermission() async {
    //获取当前的权限
    var status = await Permission.bluetooth.status;
    var status2 = await Permission.bluetoothAdvertise.status;
    var status3 = await Permission.bluetoothConnect.status;
    var status4 = await Permission.bluetoothScan.status;

    if (status == PermissionStatus.granted ||
        status2 == PermissionStatus.granted ||
        status3 == PermissionStatus.granted ||
        status4 == PermissionStatus.granted) {
      //已经授权
      return true;
    } else {
      //未授权则发起一次申请
      Map<Permission, PermissionStatus> statues = await [
        Permission.bluetooth,
        Permission.bluetoothAdvertise,
        Permission.bluetoothConnect,
        Permission.bluetoothScan,
      ].request();

      if (statues[Permission.bluetooth] == PermissionStatus.granted ||
          statues[Permission.bluetooth] == PermissionStatus.granted ||
          statues[Permission.bluetooth] == PermissionStatus.granted ||
          statues[Permission.bluetooth] == PermissionStatus.granted) {
        return true;
      } else {
        return false;
      }
    }
  }

  // 获取外部存储权限
  static Future<bool> requestManageExternalStoragePermission() async {
    //获取当前的权限
    var status = await Permission.manageExternalStorage.status;

    if (status == PermissionStatus.granted) {
      //已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.accessNotificationPolicy.status;
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        return false;
      }
    }
  }

  static Future<bool> requestActivityRecognitionPermission() async {
    //获取当前的权限
    var status = await Permission.activityRecognition.status;

    if (status == PermissionStatus.granted) {
      //已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.activityRecognition.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        return false;
      }
    }
  }

  static Future<bool> requestAppTrackingTransparencyPermission() async {
    //获取当前的权限
    var status = await Permission.appTrackingTransparency.status;

    if (status == PermissionStatus.granted) {
      //已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.appTrackingTransparency.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        return false;
      }
    }
  }

  // 获取日历权限
  static Future<bool> requestCalendarPermission() async {
    //获取当前的权限
    var status = await Permission.calendar.status;

    if (status == PermissionStatus.granted) {
      //已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.calendar.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        return false;
      }
    }
  }

  // 获取相机权限
  static Future<bool> requestCameraPermission() async {
    //获取当前的权限
    var status = await Permission.camera.status;

    if (status == PermissionStatus.granted) {
      //已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.camera.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        return false;
      }
    }
  }

  // 获取电话权限
  static Future<bool> requestContactsPermission() async {
    //获取当前的权限
    var status = await Permission.contacts.status;

    if (status == PermissionStatus.granted) {
      //已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.contacts.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        return false;
      }
    }
  }

  // 获取相册权限
  static Future<bool> requestPhotosPermission() async {
    //获取当前的权限
    var status = await Permission.photos.status;

    if (status == PermissionStatus.granted) {
      //已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.photos.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        return false;
      }
    }
  }
}
